package com.netsdk.lht2.handle.support;

import com.netsdk.demo.util.DateUtil;
import com.netsdk.demo.util.sql.EventDto;
import com.netsdk.lht2.handle.EventHandle;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.structure.NET_TRAFFICCAR_INFO_EXTERN;
import com.sun.jna.Pointer;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Date;

/*
* 交通拥堵
* */
public class EVENT_IVS_TRAFFICJAM_HANDLE extends EventHandle {

    @Override
    public void handle(int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize, Pointer dwUser, int nSequence, Pointer reserved) throws UnsupportedEncodingException {
        EventDto dto = new EventDto();
        System.out.println("交通拥堵事件, code = " + dwAlarmType);
        dto.setEvent_name("交通拥堵事件");
        dto.setEvent_code(dwAlarmType+"");
        NetSDKLib.DEV_EVENT_TRAFFICJAM_INFO msg = new NetSDKLib.DEV_EVENT_TRAFFICJAM_INFO();
        ToolKits.GetPointerData(pAlarmInfo, msg);
        // 通道号
        String nChannelID = getChannelMapping(msg.nChannelID);
        dto.setChannel(nChannelID);
        System.out.println("nChannelID:" + msg.nChannelID);
        // 车道号
        int nLane = msg.nLane;
        System.out.println("nLane:" + nLane);
        dto.setLane(nLane+"");
        // 报警时间间隔
        int nAlarmIntervalTime = msg.nAlarmIntervalTime;
        System.out.println("nAlarmIntervalTime:" + nAlarmIntervalTime);
        dto.setnAlarmIntervalTime(nAlarmIntervalTime+"");
        //事件名称
        byte[] szName = msg.szName;
        System.out.println("szName UTF-8:" + new String(szName,"UTF-8").trim());
        System.out.println("szName GBK:" + new String(szName,"GBK").trim());
        dto.setSzName(new String(szName,"UTF-8").trim());
        //相对事件时间戳(单位是毫秒)
        double pts = msg.PTS;
        System.out.println("pts:" + new Date((long) pts));
        dto.setEvent_time(DateUtil.format(new Date((long) pts), "yyyy-MM-dd HH:mm:ss.SSS"));

        //事件id
        int nEventID = msg.nEventID;
        System.out.println("nEventID:" + nEventID);
        dto.setEvent_id(nEventID+"");
        //图片保存
        if (pBuffer != null && dwBufSize > 0 && msg.stCommInfo.nPictureNum > 0) {
            System.out.println(msg.stCommInfo.nPictureNum + " pictures received!");
            for(int i = 0; i < msg.stCommInfo.nPictureNum; i ++){
                String pName = System.currentTimeMillis() + "related_TrafficJam_common_" + i + ".jpg";
                String bigPicture = picturePath + "\\" + pName;
                if(null != pBuffer){
                    dto.setBigPicture(StringUtils.isNotEmpty(dto.getBigPicture()) ? dto.getBigPicture() + ","+pName : ""+pName );
                }

                ToolKits.savePicture(pBuffer, msg.stCommInfo.stuPicInfos[i].nOffset, msg.stCommInfo.stuPicInfos[i].nLength, bigPicture);
            }
        }
        //交通车辆信息扩展
        NET_TRAFFICCAR_INFO_EXTERN stInfo = new NET_TRAFFICCAR_INFO_EXTERN();
        ToolKits.GetPointerData(msg.stTrafficCar.pTrafficInfoExtern,stInfo);
        System.out.println("stInfo.bAverageSpeed = " + stInfo.bAverageSpeed);
        System.out.println("stInfo.bSpaceOcpRatio = " + stInfo.bSpaceOcpRatio);
        System.out.println("stInfo.dAverageSpeed = " + stInfo.dAverageSpeed);
        System.out.println("stInfo.dSpaceOcpRatio = " + stInfo.dSpaceOcpRatio);
        System.out.println("stInfo.nContainerNumber = " + stInfo.nContainerNumber);
        System.out.println("stInfo.nContainerTextNum = " + stInfo.nContainerTextNum);
        String nContainerTextNum = "";
        for(int i = 0; i < stInfo.nContainerTextNum; i ++){
            byte[] tmp = new byte[64];
            System.arraycopy(stInfo.szContainerText, i * 64, stInfo.nContainerTextNum, 0, 64);
            String text = new String(tmp).trim();
            System.out.println("stInfo.nContainerTextNum[" + i + "] = " + text);
            nContainerTextNum = nContainerTextNum + text + ",";
        }
        dto.setnContainerTextNum(nContainerTextNum);
        // 数据入库
        insertEventDto(dto);
    }
}
